package com.icefish.utils;

import java.sql.Connection;
import java.sql.DriverManager;

public class DatabaseConnFactory {

    private static String driver = "com.mysql.jdbc.Driver";
    private static String url = "jdbc:mysql://localhost:3306/sports_shop?characterEncoding=UTF-8&useSSL=false&useServerPrepStmts=true";
    private static String username = "root";
    private static String password = "123456";
    private static Connection conn;

    public static Connection getConn() {

        try {
            Class.forName(driver);
            conn = DriverManager.getConnection(url,username,password);
        }
        catch (Exception e) {
            System.out.println("----------------错误！DB-连接数据库！错误！----------------");
            System.out.println(e);
            System.out.println("----------------错误！DB-连接数据库！错误！----------------");
        }

        return conn;
    }

    // 对于配置信息，只进行一次初始化
    static {
        try {
//            private Properties p = new Properties();
//            InputStream is = this.getClass().getResourceAsStream("./config.ini");
//            p.load(is);
//            driver = p.getProperty("driver").replaceAll ("/", ".");
//            url = p.getProperty("url");
//            username = p.getProperty("username");
//            password = p.getProperty("password");
        }
        catch (Exception e) {
            System.out.println("----------------错误！读取sql配置文件！错误！----------------");
            System.out.println(e);
            System.out.println("----------------错误！读取sql配置文件！错误！----------------");
        }
    }
}


